//
//  HLOtherRemidAttach.m
//  YueYue
//
//  Created by hulu001 on 2023/6/13.
//

#import "HLOtherRemidAttach.h"
#import <YYtext/YYText.h>

@implementation HLOtherRemidAttach

- (nonnull NSString *)encodeAttachment {
    NSDictionary *dict = @{
                           @"msgType" : @(self.msgType),
                           @"ext" : @{
                               @"content" : self.content?:@"",
                               @"messageContent": self.messageContent?:@"",
                               @"messageTitle": self.messageTitle?:@"",
                               @"tittle" : self.tittle?:@""
                           }
                           };
    
    NSData *data = [NSJSONSerialization dataWithJSONObject:dict
                                                   options:0
                                                     error:nil];
    NSString *content = nil;
    if (data) {
        content = [[NSString alloc] initWithData:data
                                        encoding:NSUTF8StringEncoding];
    }
    return content;
}

- (CGSize)contentSize:(NIMMessage *)message cellWidth:(CGFloat)width
{
    NIMCustomObject *object = (NIMCustomObject *)message.messageObject;
    HLOtherRemidAttach *attachment = (HLOtherRemidAttach *)object.attachment;
    
//    NSString *content = attachment.content;
//    CGSize size = [content boundingRectWithSize:CGSizeMake(230, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
    
    YYLabel *contentLabel = [YYLabel.alloc initWithFrame:CGRectMake(0, 0, 155, 50)];
    contentLabel.font = [UIFont systemFontOfSize:12];
    contentLabel.numberOfLines = 0;
    NSMutableAttributedString *attString = [NSMutableAttributedString.alloc initWithString:[NSString stringWithFormat:@"%@ %@", attachment.tittle,attachment.content]];
    if (attachment.msgType == 20 || attachment.msgType == 21){
        attString = [NSMutableAttributedString.alloc initWithString:attachment.messageContent];
    }
    contentLabel.attributedText = attString;
    CGSize size = [contentLabel sizeThatFits:CGSizeMake(230, MAXFLOAT)];
    
    return CGSizeMake(size.width + 52, size.height + 16);
}

- (NSString *)cellContent:(NIMMessage *)message
{
    return @"HLOtherRemindContentView";
}

- (UIEdgeInsets)cellInsets:(NIMMessage *)message
{
    CGFloat left = (SCREEN_WIDTH - [self contentSize:message cellWidth:0].width) * 0.5;
    return UIEdgeInsetsMake(8, left, 28, left);
}

- (UIEdgeInsets)contentViewInsets:(NIMMessage *)message
{
    return UIEdgeInsetsZero;
}

- (BOOL)shouldShowAvatar:(NIMMessage *)message
{
    return NO;
}
@end
